package com.slkj.shilixiaoyuanapp.ui.teacher.tool.meeting;

import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.MeetSqrAndPeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.PeopleEntity;
import com.slkj.shilixiaoyuanapp.entity.ToolMeetingDetailEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity;
import com.slkj.shilixiaoyuanapp.ui.common.ChosePeopleActivity__JumpCenter;
import com.slkj.shilixiaoyuanapp.util.TimeUtils;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.PickerViewProvider;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import com.tkk.api.RxEventProcessor;
import com.tkk.api.annotation.Recipient;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

import static com.slkj.shilixiaoyuanapp.entity.code.RxEventProcessorTag.CHOSE_SPR_FINSH;

@ActivityInfo(layout = R.layout.activity_meeting_wtg, title = "未通过")
public class MeetingWtgActivity extends BaseActivity {
    @InjectParam int id;
    @InjectParam ToolMeetingDetailEntity data;
    @BindView(R.id.tv_yhry)TextView tvYhry;
    @BindView(R.id.tv_time) TextView tvTime;
    @BindView(R.id.edit_pos) EditText editPos;
    @BindView(R.id.edit_name) EditText editName;
    @BindView(R.id.tv_spr) TextView tvSpr;
    @BindView(R.id.tv_user_name)
    TextView tvUserName;
    @BindView(R.id.tv_now_day_time)
    TextView tvNowDayTime;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.tv_remark)
    TextView tvWtg;
    MeetSqrAndPeopleEntity peopleEntity;
    public final int CHOSE_YHRY = 1;
    public final int CHOSE_SQR = 2;
    List<Integer> sqrIds = new ArrayList<>();
    List<Integer> yhryIds = new ArrayList<>();
    String time;
    public static final String tag = "MeetingWtgActivity";

    @Override
    protected void init() {
        MeetingWtgActivity__JumpCenter.bind(this);
        RxEventProcessor.get().bind(this);
        tvUserName.setText(UserRequest.getInstance().getUser().getUser().getUName());
        tvNowDayTime.setText(TimeUtils.getNowNyr());
        setViewData();
        time = data.getDatetime();
        yhryIds =  ChosePeopleActivity.getIdList(data.getTeacherName());
        sqrIds = ChosePeopleActivity.getIdList(data.getAuditor());

    }
    private void setViewData() {

        tvYhry.setText(ChosePeopleActivity.getPeoplesInfos(data.getTeacherName()));
        tvTime.setText(data.getDatetime());
        editName.setText(data.getTitle());
        editPos.setText(data.getPlace());
        editContent.setText(data.getBody());
        tvWtg.setText(data.getRefute());
        tvSpr.setText(ChosePeopleActivity.getPeoplesInfos(data.getAuditor()));

    }

    @OnClick(R.id.layout_chose_yhry)
    public void onLayoutChoseYhryClicked() {
        if (peopleEntity == null) {
            getSqrAndPeople(CHOSE_YHRY);
        } else {
            ChosePeopleActivity__JumpCenter.builder(this).setTag(tag).setCode(CHOSE_YHRY).setData(peopleEntity.getTeacher()).create().go();
        }
    }

    @OnClick(R.id.layout_chose_time)
    public void onLayoutChoseTimeClicked() {
        PickerViewProvider.getTimePicker(this, new OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {
                time = TimeUtils.dateToStrM(date);
                tvTime.setText(time);
            }
        }).show();
    }

    @OnClick(R.id.layout_chose_spr)
    public void onLayoutChoseSprClicked() {
        if (peopleEntity == null) {
            getSqrAndPeople(CHOSE_SQR);
        } else {
            ChosePeopleActivity__JumpCenter.builder(this).setTag(tag).setCode(CHOSE_SQR).setData(peopleEntity.getAuditor()).create().go();
        }
    }

    /**
     * 选择发布对象或者与会人员完成
     * @param personEntityList
     * @param info
     */
    /**
     * @param code
     * @param info
     * @param peopleEntityList
     * @param checkIds
     */
    @Recipient(tag = tag+ CHOSE_SPR_FINSH)
    void onChoseObjFinsh(int code, String info, List<PeopleEntity> peopleEntityList, List<Integer> checkIds) {
        if (code == CHOSE_YHRY) {
            tvYhry.setText(checkIds.size() == 0 ? "" : info);
            yhryIds.clear();
            yhryIds.addAll(checkIds);
            peopleEntity.getTeacher().clear();
            peopleEntity.getTeacher().addAll(peopleEntityList);
        } else {
            tvSpr.setText(checkIds.size() == 0 ? "" : info);
            sqrIds.clear();
            sqrIds.addAll(checkIds);
            peopleEntity.getAuditor().clear();
            peopleEntity.getAuditor().addAll(peopleEntityList);
        }


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        RxEventProcessor.get().unBind(this);
    }

    /**
     * 选择审批人
     *
     * @param type
     */
    private void getSqrAndPeople(int type) {
        HttpHeper.get().toolService()
                .getMeetPeople()
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<MeetSqrAndPeopleEntity>(true,this) {
                    @Override
                    public void onCallBackSuccess(MeetSqrAndPeopleEntity data) {
                        peopleEntity = data;
                        ChosePeopleActivity__JumpCenter.builder(MeetingWtgActivity.this)
                                .setCode(type)
                                .setTag(tag)
                                .setData(type == CHOSE_YHRY ? peopleEntity.getTeacher() : peopleEntity.getAuditor())
                                .create()
                                .go();
                    }
                });
    }

    @OnClick(R.id.tv_up)
    void up() {
        if (yhryIds.size() == 0) {
            showToast("请选择与会人员");
            return;
        }
        if (TextUtils.isEmpty(time)) {
            showToast("请选择会议时间");
            return;
        }
        String pos = editPos.getText().toString();
        if (TextUtils.isEmpty(pos)) {
            showToast("请输入会议地点");
            return;
        }
        String name = editName.getText().toString();
        if (TextUtils.isEmpty(name)) {
            showToast("请输入会议名称");
            return;
        }
        String context = editContent.getText();
        if (TextUtils.isEmpty(context)) {
            showToast("请输入会议内容");
            return;
        }
        if (sqrIds.size() == 0) {
            showToast("请选择审批人");
            return;
        }

        HttpHeper.get().toolService()
                .addMeeting(context,name,pos,time,sqrIds.toString(),yhryIds.toString())
                .compose(bindToLifecycle())
                .compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(MeetingWtgActivity.this,data);
                    }
                });
    }

}
